#include "LinkListRectangleH.h"

#include <conio.h>
#include <iostream>
using namespace std;



   void RectangleLinkList::init()
   {
	   head = tail = NULL;
	   sizeOfList=0;
   }

   void RectangleLinkList::ClearList()
   {
	   RectangleNode * rctTemp1;
	   RectangleNode * rctTemp2;

	   rctTemp2 = tail;
	   rctTemp1 = rctTemp2;

	   while(rctTemp1)
	   {
		   rctTemp1 = rctTemp2->prev;
		   delete rctTemp2;
	   }
   }
  


   void RectangleLinkList::AddRecToTail(Rectangl * rct) //receive pointer to allocated Rectangle object and build a new Node and list it to the List.
   {
	   RectangleNode * rctNode = new RectangleNode;
	   rctNode->data = rct;

	   sizeOfList++;

	   if(tail == NULL)
	   {
		   rctNode->next = rctNode->prev = NULL;
		   tail = rctNode;
		   head = tail;
		   return;
	   }

	   tail->next = rctNode;
	   rctNode->prev = tail;
	   rctNode->next = NULL;
	   tail = rctNode;
	   return;
   }

   bool RectangleLinkList::RemoveRecNodeFromList(Rectangl * rct) //receive pointer to allocated Rectangle object and remove node from List and delete object.
   {
	   RectangleNode * rctNodeTemp;
	   
	   rctNodeTemp = tail;

	   while(rctNodeTemp) 
	   {
	     
		   if(rctNodeTemp->data == rct)
	       {
			   if(!rctNodeTemp->prev) //case the rectangle node is the head
			   {
					   head = tail = rctNodeTemp->next;
					   if(rctNodeTemp->next) rctNodeTemp->next->prev = NULL;
				
			   }
			   else if(!rctNodeTemp->next) //case the rectangle node is the tail
				    {
				 	   tail = rctNodeTemp->prev;
					   rctNodeTemp->prev->next=NULL;
			        }
					else //case the rectangle node is in the middle
					{
                     rctNodeTemp->prev->next = rctNodeTemp->next;
			         rctNodeTemp->next->prev = rctNodeTemp->prev;
					}
			 delete rctNodeTemp;
			 sizeOfList--;
			 return true;
		   }

		   rctNodeTemp=rctNodeTemp->prev;
	   }

	   return false;
   }


   void RectangleLinkList::DrawList(int drawingOption) //Draw the entire List. 2 drawing options are available
   {
	   RectangleNode * rctNodeTemp;
	   
	   rctNodeTemp = head;

	   if(!rctNodeTemp) cerr << "Sorry the list is empty";

	   while(rctNodeTemp)
	   {
		   rctNodeTemp->data->goAndDrawRectangle(drawingOption);
		   rctNodeTemp=rctNodeTemp->next;
	   }
   }


   int RectangleLinkList::GetsizeOfList()
   {
	   return sizeOfList;
   }


   bool RectangleLinkList::FindRectangleInListAngGetOut(int x, int y, Rectangl ** recOut)
   {
	   RectangleNode * rctNodeTemp;

	   int x_temp,y_temp;

	   rctNodeTemp = tail;

	   while(rctNodeTemp)
	   {
		   rctNodeTemp->data->getPos(x_temp,y_temp);
		   
		   if((x_temp == x) && (y_temp == y))
			 {
				*recOut = rctNodeTemp->data;
				return true;
		     }
		   
		   rctNodeTemp = rctNodeTemp->prev;
	   }

	   *recOut = NULL; 
	   _getch();
	   return false;
   }


   bool RectangleLinkList::checkPointInListOfRectangles(Rectangl ** recOut, int x, int y)
   {
	   RectangleNode * rctNodeTemp;
	   bool flag = true;
	 
	   rctNodeTemp = tail;

	   while(rctNodeTemp)
	   {
		   if(rctNodeTemp->data->pointInRectangle(x,y))
		   {
			   *recOut = rctNodeTemp->data;
			   return true;
		   }

		   rctNodeTemp = rctNodeTemp->prev;
	   }
	 
	   *recOut = NULL;
	   return false;
   }


void RectangleLinkList::drawPointsInListOfRectangles(Rectangl * inputRec){

//the function receives Rectangle and go over the entire list starting from tail doing prev all the way
//until NULL. every Rectangle in the way that touchs the input Rectangle 
//you send it to printing.

	RectangleNode * node;
	node = tail;
	while(node){
		if (node->data != inputRec) //Prevent comparing rectangle with himself.
			if (inputRec->isOverlap(node->data))
				node->data->goAndDrawRectangle(); // if overlap then print the overlap rectangle
		node = node->prev;
	}

}

bool RectangleLinkList::isEmptyList()
{

	return (head == NULL);
}

void RectangleLinkList::insertNodeToTail(RectangleNode * newTail){
	newTail->next = NULL;
	if (isEmptyList())
		head = tail = newTail;
	else
	{
		tail->next = newTail;
		tail = newTail;
	}
}

void RectangleLinkList::insertNodeToHead(RectangleNode* newHead){
		
	newHead->next = head;
	if (tail == NULL)
		tail = newHead;
	head = newHead;
}


void RectangleLinkList::moveToHead(RectangleNode * node){
	//TODO:FUTURE REQ

}